package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.dataProvide.OutputProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.Date;

/**
 * Created by hacker on 2017/6/22.
 */

public class BackBillOptionActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener {
    private ImageView arrowImageView;
    private ExpandView expandView;
    private TextView receivedCompanyTV;
    private TextView outTimeTV;
    private TextView grossLabelTV;
    private TextView grossWeightTV;
    private TextView treLabelTV;
    private TextView treWeightTV;
    private TextView netLabelTV;
    private TextView netWeightTV;
    private TextView billNumberTV;
    private TextView supplyCompanyTV;
    private TextView coalNameTV;

    private TextView carInformationTV;
    private FloatingActionButton saveFab;

    private EditText grossWeightET;
    private EditText treWeightET;
    private EditText netWeightET;
    private EditText lossWeightET;
    private EditText remarkET;
    private EditText deductWeightET;
    private OutputBill mOutputBill;
    private TextView carNumberTV;
    private TextView obNumberTV;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.back_bill_option);
        super.initStatusBar(true);

        initData();
        initView();
        initEvent();
        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle(getResources().getString(R.string.back_bill_entrance));
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        saveFab = findViewById(R.id.save_fab);

        expandView = findViewById(R.id.expandView);
        arrowImageView = findViewById(R.id.arrow);
        receivedCompanyTV = findViewById(R.id.received_company);
        outTimeTV = findViewById(R.id.out_time);
        grossLabelTV = findViewById(R.id.gross_label);
        grossWeightTV = findViewById(R.id.gross_weight);
        treLabelTV = findViewById(R.id.tre_label);
        treWeightTV = findViewById(R.id.tre_weight);
        netLabelTV = findViewById(R.id.net_label);
        netWeightTV = findViewById(R.id.net_weight);
        billNumberTV = findViewById(R.id.bill_number);
        supplyCompanyTV = findViewById(R.id.supply_company);
        coalNameTV = findViewById(R.id.coal_name);
        carInformationTV = findViewById(R.id.car_information);
        grossWeightET = findViewById(R.id.received_gross_weight);
        treWeightET = findViewById(R.id.received_tre_weight);
        netWeightET = findViewById(R.id.received_net_weight);
        lossWeightET = findViewById(R.id.loss_weight);
        deductWeightET = findViewById(R.id.deduct_weight);
        remarkET = findViewById(R.id.remark);
        carNumberTV = findViewById(R.id.car_number);
        obNumberTV = findViewById(R.id.ob_number);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        mOutputBill = (OutputBill) bundle.getSerializable("backBill");
    }


    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(view -> BackBillOptionActivity.this.finish());
        arrowImageView.setOnClickListener(this);
        saveFab.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        receivedCompanyTV.setText(mOutputBill.getBuycompany());
        outTimeTV.setText(TimeUtils.parseDateTime(mOutputBill.getOuttime()));
        billNumberTV.setText(mOutputBill.getObnum());
        supplyCompanyTV.setText(mOutputBill.getSendcompany());
        coalNameTV.setText(mOutputBill.getCoaltype());
        grossWeightTV.setText(mOutputBill.getSendgrossweight() + "");
        treWeightTV.setText(mOutputBill.getSendtareweight() + "");
        netWeightTV.setText(mOutputBill.getSendnetweight() + "");
        carInformationTV.setText(mOutputBill.getCarnumber()
                + "  " + mOutputBill.getCarowner() + "  "
                + BaseActivity.formatPhoneNumber(mOutputBill.getCarconnectphone()));
        carNumberTV.setText(mOutputBill.getCarnumber());
        obNumberTV.setText(mOutputBill.getObnum());
    }

    @Override
    public void onBackPressed() {
        //alert
        super.onBackPressed();
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.arrow:
                if (expandView.isExpand()) {
                    expandView.collapse();
                    arrowImageView.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                } else {
                    expandView.expand();
                    arrowImageView.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                }
                break;
            case R.id.save_fab:
                saveBackBill();
                break;
        }
    }

    private void saveBackBill() {
        float receivedGrossWeight = 0;
        try {
            receivedGrossWeight = Float.valueOf(grossWeightET.getText().toString());
        } catch (NumberFormatException e) {
            getSweetAlertDialog(BackBillOptionActivity.this).showError("错误", "输入的收货毛重不正确");
            e.printStackTrace();
            grossWeightET.setText("");
            grossWeightET.setFocusable(true);
            return;
        }
        float receivedTreWeight = 0;
        try {
            receivedTreWeight = Float.valueOf(treWeightET.getText().toString());
        } catch (NumberFormatException e) {
            getSweetAlertDialog(BackBillOptionActivity.this).showError("错误", "输入的收货皮重不正确");
            e.printStackTrace();
            treWeightET.setText("");
            treWeightET.setFocusable(true);
            return;
        }
        float receivedNetWeight = 0;
        try {
            receivedNetWeight = Float.valueOf(netWeightET.getText().toString());
        } catch (NumberFormatException e) {
            getSweetAlertDialog(BackBillOptionActivity.this).showError("错误", "输入的收货净重不正确");
            e.printStackTrace();
            netWeightET.setText("");
            netWeightET.setFocusable(true);
            return;
        }
        float lossWeight = 0;
        try {
            lossWeight = Float.valueOf(lossWeightET.getText().toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            lossWeightET.setFocusable(false);
        }
        float deductWeight = 0;
        try {
            deductWeight = Float.valueOf(deductWeightET.getText().toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            deductWeightET.setText("");
            deductWeightET.setFocusable(false);
        }
        mOutputBill.setDeductweight(deductWeight);
        String remark = remarkET.getText().toString();
        if(remark.length()>0){
            mOutputBill.setRemark(mOutputBill.getRemark()+"  "+remark);
        }
        if (receivedGrossWeight > 0 && receivedNetWeight > 0 && receivedTreWeight > 0) {
            mOutputBill.setIsreceived(1);
            mOutputBill.setReceivedtime(new Date());
            mOutputBill.setInputgrossweight(receivedGrossWeight);
            mOutputBill.setInputtareweight(receivedTreWeight);
            mOutputBill.setInputnetweight(receivedNetWeight);
            if(lossWeight==0 || lossWeightET.getText().toString().length()<=0) {
                lossWeight = mOutputBill.getSendnetweight() -(mOutputBill.getInputnetweight());
            }
            mOutputBill.setLossweight(lossWeight);
            mOutputBill.setTranslossweight(lossWeight + deductWeight);
            OutputProvide provide = new OutputProvide(this, this);
            provide.backBillInput(mOutputBill);
        } else {

        }

    }

    @Override
    public void showMessage(String msg) {
        if (msg == "oK") {
            getSweetAlertDialog(this).showSuccess("", "保存成功，将返回列表");
            this.finish();
        } else if (msg.contains("failure")) {
            Toast.makeText(this, "回单录入失败！\n " + msg.substring(msg.indexOf("failure") + 1), Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("", 1200);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {

    }

    @Override
    public void onLoadMoreData(Object result) {

    }
}
